import { DataTypes } from 'sequelize';
import { sequelize } from '../config/database.js';

const Expert = sequelize.define('Expert', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  domain_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '所属领域ID'
  },
  external_expert_id: {
    type: DataTypes.STRING(100),
    allowNull: true,
    comment: '外部专家系统ID'
  },
  nickname: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: '专家昵称'
  },
  bio: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '专家简介'
  },
  welcome_message: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: '专家欢迎语'
  },
  classic_cases: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: '经典案例JSON数组',
    get() {
      const value = this.getDataValue('classic_cases');
      return value ? (typeof value === 'string' ? JSON.parse(value) : value) : [];
    },
    set(value) {
      this.setDataValue('classic_cases', value ? JSON.stringify(value) : null);
    }
  },
  avatar_url: {
    type: DataTypes.STRING(500),
    allowNull: true,
    comment: '头像图片URL'
  },
  display_images: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: '展示大图URL数组',
    get() {
      const value = this.getDataValue('display_images');
      return value ? (typeof value === 'string' ? JSON.parse(value) : value) : [];
    },
    set(value) {
      this.setDataValue('display_images', value ? JSON.stringify(value) : null);
    }
  },
  pricing: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: '专家提问定价配置JSON数组',
    get() {
      const value = this.getDataValue('pricing');
      return value ? (typeof value === 'string' ? JSON.parse(value) : value) : [];
    },
    set(value) {
      this.setDataValue('pricing', value ? JSON.stringify(value) : null);
    }
  },
  sort_order: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '排序权重'
  },
  is_enabled: {
    type: DataTypes.BOOLEAN,
    defaultValue: true,
    comment: '是否启用'
  },
  created_at: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW
  },
  updated_at: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW
  }
}, {
  tableName: 'experts',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  indexes: [
    {
      name: 'idx_experts_domain_id',
      fields: ['domain_id'],
    },
    {
      name: 'idx_experts_is_enabled',
      fields: ['is_enabled'],
    },
    {
      name: 'idx_experts_sort_order',
      fields: ['sort_order'],
    },
    {
      name: 'idx_experts_external_expert_id',
      fields: ['external_expert_id'],
    },
  ],
});

// 实例方法
Expert.prototype.toJSON = function() {
  const values = { ...this.get() };
  return values;
};

// 类方法
Expert.getEnabledByDomain = function(domainId) {
  return this.findAll({
    where: { 
      domain_id: domainId,
      is_enabled: true 
    },
    order: [['sort_order', 'ASC'], ['created_at', 'ASC']],
    include: [{
      association: 'domain',
      attributes: ['id', 'name']
    }]
  });
};

Expert.findByIdWithDomain = function(id) {
  return this.findByPk(id, {
    include: [{
      association: 'domain',
      attributes: ['id', 'name', 'description', 'welcome_message_1', 'welcome_message_2', 'precondition_config']
    }]
  });
};

export default Expert;
